<?php
/**
 * 登录 注册页面  验证码  多语言 邮件发送 页面
 * 设计用户就采用接口的形式
 * @author Administrator
 *
 */
class AnonymousController extends Action {
	
	/**
	 * 管理员登录口
	 */
	public function admin() {
		$this->view ()->display ( 'file:anonymous/login.tpl' );
	}
	
	/**
	 * 多语言切换
	 */
	public function language() {
		if ($_GET ['lang']) {
			//   setcookie("lang", "");
			setcookie ( "lang", trim ( $_GET ['lang'] ), time () + 3600 * 8, "/" );
			echo "<script>javascript:history.go(-1)</script>";
			exit ();
		
		}
	}
	
	/**
	 * 用户注册  接口  调用用户服务
	 * 只检测提交的字段是否合法
	 * @todo 默认用户组未添加
	 */
	public function register() {
		if ($_POST) {
			// 包含两部分 1原实体字段 2扩展字段
			$userInfo = $_POST;
			$this->checkValidate ( $_POST ['codeimg'] );
			
			if (! $this->filter ( $userInfo )) {
				$this->sendNotice ( '存在非法字符!', null, false );
			}
			
			$rs = self::getMemberService ()->add ( $userInfo );
			
			if ($rs != "success") {
				$this->sendNotice ( $rs, null, false );
			}
			
			$this->redirect ( '注册成功,请先登录!', '/anonymous/login', 1 );
		} else {
			$group = self::getMemberService ()->getMemberGroup ();
			
			$this->view ()->assign ( "group", $group );
			$this->view ()->display ( 'mysql:register.tpl' );
		}
	}
	
	/**
	 * 会员登录
	 */
	public function login() {
		//导入推荐用户
		$userface = self::getMemberService ()->getRandFace ( 12 );
		$this->view ()->assign ( 'face', $userface );
		if (isset ( $_COOKIE ['user'] )) {
			$userInfo = self::getMemberService ()->getOneMemberByUsername ( $_COOKIE ['user'] );
			$_SESSION ['uid'] = serialize ( array_merge ( array ('uid' => $userInfo ['uid'], 'username' => $userInfo ['username'], 'groupid' => $userInfo ['groupid'], 'verify' => $userInfo ['verify'], 'realname' => $userInfo ['real_name'] ) ) );
			$this->view ()->assign ( 'user', $userInfo );
			$this->view ()->assign ( "userinfo", $userInfo );
		}
		$this->view ()->display ( 'mysql:login.tpl' );
	}
	
	/**
	 * 生成验证码
	 */
	public function validate() {
		require 'ValidateCode.php'; // 先把类包含进来，实际路径根据实际情况进行修改。
		unset ( $_SESSION ['authum_session'] ); // 每次都能娶到新的值
		$_vc = new ValidateCode (); // 实例化一个对象
		$_vc->doimg ();
		$_SESSION ['authnum_session'] = $_vc->getCode (); // 验证码保存到SESSION中
	}
	
	/**
	 * 登录验证
	 */
	public function check() {
		$logininfo = $_POST;
		// 验证码
		if (! $this->checkValidate ( $_POST ['codeimg'] )) {
			$this->sendNotice ( "验证码错误", null );
		}
		
		if ($this->filter ( $logininfo )) {
			$this->sendNotice ( '存在非法字符!', null );
		}
		$userInfo = self::getMemberService ()->isLogin ( $logininfo ['mobile_phone'], $logininfo ['password'] );
		
		if (! is_array ( $userInfo )) {
			$this->sendNotice ( $userInfo, null );
		}
		//增加登录历史记录
		// 生成识别号 用户id和和用户名
		//默认记录8个小时
		setcookie ( "user", $userInfo ['username'], time () + 3600 * 10, "/" );
		
		self::getMemberService ()->addLoginLog ( $userInfo );
		$this->sendNotice ( "ok", null, true );
	}
	
	/**
	 * 退出登录 同步登录
	 */
	public function signout() {
		setcookie ( "user", "", - 86400, "/" );
		unset ( $_SESSION ['uid'] );
		$this->redirect ( "退出成功!", '/anonymous/login' );
	}
	/**
	 * 
	 * 验证非法字符
	 * @param boolean $arr
	 */
	private function filter($arr) {
		foreach ( $arr as $v ) {
			if (preg_match ( "#[.\\\/~\?\&\*]#is", $v )) {
				return true;
			}
		
		}
		return false;
	
	}
	
	/**
	 * 检测验证码是否正确
	 */
	private function checkValidate($codeimg) {
		if (! isset ( $codeimg )) {
			return false;
		}
		if (trim ( $codeimg ) != $_SESSION ["authnum_session"]) {
			return false;
		}
		return true;
	}
	
	/**
	 * 获取用户服务类
	 */
	public static function getMemberService() {
		return new MemberService ();
	}

}